
from abs_translate.get_abs import DumpJson,show_abs
import os
import json
from meta_service.store import ArxivPaper


def GetOriginAbs(entry_id:str):
    paper = ArxivPaper(entry_id)
    return paper.get_summary()

def RequestTranslatedAbs(entry_id:str):
    file_path = os.path.join("output/abs_json_dump",f"{entry_id}.json")
    info = "获取翻译数据失败"
    try:
        # 文件不存在
        if not os.path.exists(file_path):
            DumpJson(entry_id)
        # 读取文件
        with open(file_path, 'r', encoding="utf-8") as fp:
            info = json.load(fp)
            info = str(info["summary_zh"])
    except Exception as e:
        print("RequestTranslatedAbs error : " + str(e))
        info = "获取翻译数据失败"
    return info

def GetOrInitTagForAllPkl(dir_path:str="data/"):
    from meta_service.tag import ArxivTag,InitTag
    from meta_service.store import ArxivPaper
    import os
    for file_name in os.listdir(dir_path):
        entry_id = file_name.split(".pkl")[0]
        paper = ArxivPaper(entry_id)
        tag = InitTag(paper)
        # 生成tag
        header = file_name.split(".")[0]
        year = "unkown"
        month = "unkown"
        if len(header) == 4:
            year = "20" + str(header[:2])
            month = str(header[2:])
        tag.AddTag("year",year)
        tag.AddTag("month",month)
        tag.AddTag("entry_id",str(paper.get_entry_id()))
        # tag.SaveToDB()

